package com.car.util;

import java.io.IOException;
import java.net.MalformedURLException;

import org.apache.log4j.Logger;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;
import com.gargoylesoftware.htmlunit.NicelyResynchronizingAjaxController;
import com.gargoylesoftware.htmlunit.TextPage;
import com.gargoylesoftware.htmlunit.WebClient;

public class DocUtil {
	static Logger logger = Logger.getLogger(DocUtil.class.getName());

	// 下载网页，获得doc
	public static Document getDocFromTextPage(String url, boolean js, boolean ajax) {
		logger.info("d-------url:" + url);
		WebClient webClient = new WebClient(BrowserVersion.FIREFOX_38);

		webClient.getOptions().setThrowExceptionOnScriptError(false);
		webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);
		webClient.getOptions().setTimeout(5000);
		webClient.getOptions().setCssEnabled(false);
		if (js) {
			webClient.setJavaScriptTimeout(5000);
			webClient.getOptions().setJavaScriptEnabled(true);
			webClient.waitForBackgroundJavaScript(10000);
		}
		if (ajax)
			webClient.setAjaxController(new NicelyResynchronizingAjaxController());

		TextPage page = null;
		try {
			page = webClient.getPage(url);
		} catch (FailingHttpStatusCodeException e) {
		} catch (MalformedURLException e) {
		} catch (IOException e) {
		} finally {
			webClient.close();
		}

		return Jsoup.parse(page.getContent());

	}

	/**
	 * download page
	 * 
	 * @param url
	 * @return Document
	 * @throws Exception
	 */
	public static Document getDoc(String url) {

		Document doc = null;
		int i = 0;
		while (i < 5) {
			try {
				doc = Jsoup.connect(url).timeout(10000).get();
				logger.info("download success:" + url);
				break;
			} catch (Exception e) {
				if (i > 2) {
					logger.info("download wrong :" + url);
				}
				i++;
				if (i >= 5)
					try {
						throw new Exception(e);
					} catch (Exception e1) {
<<<<<<< HEAD
=======
						e1.printStackTrace();
>>>>>>> 12cebf820d4efd66d730623df0b55f6b98421d88
					}
			}
		}
		return doc;
	}

}
